<?php
/**
 * Class that operate on table 'businesses_category'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class BusinessesCategoryMySqlDAO implements BusinessesCategoryDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return BusinessesCategoryMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM businesses_category WHERE businesscat_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAllSearchBy($businessesCategory){
		$sql = 'SELECT * FROM businesses_category WHERE '.$businessesCategory->search_by.' like"%'.$businessesCategory->search_by_value.'%" order by '.$businessesCategory->order_by.' '.$businessesCategory->sort_order.' limit '.$businessesCategory->results_per_page.' offset '.$businessesCategory->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function queryAll($businessesCategory){
		$sql = 'SELECT * FROM businesses_category order by '.$businessesCategory->order_by.' '.$businessesCategory->sort_order.' limit '.$businessesCategory->results_per_page.' offset '.$businessesCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	public function queryAllCount($businessesCategory){
		$sql = 'SELECT count(*) FROM businesses_category order by '.$businessesCategory->order_by.' '.$businessesCategory->sort_order.' limit '.$businessesCategory->results_per_page.' offset '.$businessesCategory->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function SearchByCount($businessesCategory){
		$sql = 'SELECT count(*) FROM businesses_category WHERE '.$businessesCategory->search_by.' like"%'.$businessesCategory->search_by_value.'%" order by '.$businessesCategory->order_by.' '.$businessesCategory->sort_order.' limit '.$businessesCategory->results_per_page.' offset '.$businessesCategory->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM businesses_category ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param businessesCategory primary key
 	 */
	public function delete($businesscat_id){
		$sql = 'DELETE FROM businesses_category WHERE businesscat_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($businesscat_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param BusinessesCategoryMySql businessesCategory
 	 */
	public function insert($businessesCategory){
		$sql = 'INSERT INTO businesses_category (admin_id, category_name, category_content, status, created_date, updated_date) VALUES (?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($businessesCategory->adminId);
		$sqlQuery->set($businessesCategory->categoryName);
		$sqlQuery->set($businessesCategory->categoryContent);
		$sqlQuery->setNumber($businessesCategory->status);
		$sqlQuery->set($businessesCategory->createdDate);
		$sqlQuery->set($businessesCategory->updatedDate);

		$id = $this->executeInsert($sqlQuery);	
		$businessesCategory->businesscatId = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param BusinessesCategoryMySql businessesCategory
 	 */
	public function update($businessesCategory){
		$sql = 'UPDATE businesses_category SET admin_id = ?, category_name = ?, category_content = ?, status = ?, created_date = ?, updated_date = ? WHERE businesscat_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($businessesCategory->adminId);
		$sqlQuery->set($businessesCategory->categoryName);
		$sqlQuery->set($businessesCategory->categoryContent);
		$sqlQuery->setNumber($businessesCategory->status);
		$sqlQuery->set($businessesCategory->createdDate);
		$sqlQuery->set($businessesCategory->updatedDate);

		$sqlQuery->setNumber($businessesCategory->businesscatId);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM businesses_category';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByAdminId($value){
		$sql = 'SELECT * FROM businesses_category WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCategoryName($value){
		$sql = 'SELECT * FROM businesses_category WHERE category_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCategoryContent($value){
		$sql = 'SELECT * FROM businesses_category WHERE category_content = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM businesses_category WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM businesses_category WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM businesses_category WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByAdminId($value){
		$sql = 'DELETE FROM businesses_category WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCategoryName($value){
		$sql = 'DELETE FROM businesses_category WHERE category_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCategoryContent($value){
		$sql = 'DELETE FROM businesses_category WHERE category_content = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM businesses_category WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM businesses_category WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM businesses_category WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return BusinessesCategoryMySql 
	 */
	protected function readRow($row){
		$businessesCategory = new BusinessesCategory();
		
		$businessesCategory->businesscatId = $row['businesscat_id'];
		$businessesCategory->adminId = $row['admin_id'];
		$businessesCategory->categoryName = $row['category_name'];
		$businessesCategory->categoryContent = $row['category_content'];
		$businessesCategory->status = $row['status'];
		$businessesCategory->createdDate = $row['created_date'];
		$businessesCategory->updatedDate = $row['updated_date'];

		return $businessesCategory;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return BusinessesCategoryMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>